package com.example.day8;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    Button zifu, zijie,tupian;
    TextView text;
    ImageView image;
    ScrollView scroll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String fileName1 = "new1.txt";
        final String fileName2 = "17.txt";
        final String fileName3 = "text.png";
        scroll=findViewById(R.id.scroll1);
        zifu = findViewById(R.id.zifu);
        zijie = findViewById(R.id.zijie);
        tupian=findViewById(R.id.tupian);
        text = findViewById(R.id.text);
        image=findViewById(R.id.image);
        scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.e("#####X",scrollX+"");
                Log.e("#####Y",scrollY+"");
                Log.e("#####Ox",oldScrollX+"");
                Log.e("#####OY",oldScrollY+"");
            }
        });
        tupian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AssetManager assetM = getAssets();
                try {
                    InputStream in = assetM.open(fileName3);
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    image.setImageBitmap(bitmap);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        zifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AssetManager assetM = getAssets();
                try {
                    InputStream in = assetM.open(fileName1);
                    byte[] data = new byte[in.available()];
                    in.read(data);
                    String aa = new String(data, 0, data.length);
                    text.setText(aa);
                    Log.e("########", aa);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        zijie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName2));
                    BufferedReader bufReader = new BufferedReader(inputReader);
                    String line = "";
                    StringBuffer result = new StringBuffer();
                    while (true) {
                        if (!((line = bufReader.readLine()) != null)) {
                            break;
                        }
                        result.append(line);
                    }
                    text.setText(result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
